package com.guowen.myaccount.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.guowen.myaccount.AccManagementActivity;
import com.guowen.myaccount.AccountContextActivity;
import com.guowen.myaccount.MainActivity;
import com.guowen.myaccount.R;
import com.guowen.myaccount.RecordActivity;
import com.guowen.myaccount.adapter.AccountAdapter;
import com.guowen.myaccount.db.AccManageBean;
import com.guowen.myaccount.db.AccountBean;
import com.guowen.myaccount.db.DBManager;
import com.guowen.myaccount.frag_history.MonthFragment;

import java.io.Serializable;
import java.util.List;

public class ChoiceDialog extends Dialog implements View.OnClickListener{

    private int position;
    private TextView delTv, conTv, altTv, chaTv;
    private List<AccountBean> mDatas;
    private AccountAdapter adapter;
    public AccountBean click_bean;

    public ChoiceDialog(@NonNull Context context) {
        super(context);
    }

    public interface onAlterListener{
        void onAlter(AccountBean bean);
    }

    onAlterListener onAlterListener;

    public void setOnAlterListener(onAlterListener onAlterListener){
        this.onAlterListener = onAlterListener;
    }

    public ChoiceDialog(@NonNull Context context, int position, List<AccountBean> mDatas, AccountAdapter adapter) {
        super(context);
        this.position = position;
        this.mDatas = mDatas;
        this.adapter = adapter;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_choice);
        delTv = findViewById(R.id.dialog_choice_delete);
        conTv = findViewById(R.id.dialog_choice_context);
        altTv = findViewById(R.id.dialog_choice_alter);
        chaTv = findViewById(R.id.dialog_choice_changed);

        delTv.setOnClickListener(this);
        conTv.setOnClickListener(this);
        altTv.setOnClickListener(this);
        chaTv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        click_bean = mDatas.get(position-1);
        Intent intent = null;
        switch (v.getId()) {
            case R.id.dialog_choice_delete:
                mDatas.remove(click_bean);
                List<AccManageBean> list = DBManager.getAccManList();
                AccManageBean bean = null;
                for (int i = 0; i < list.size(); i++){
                    bean = list.get(i);
                    if (bean.getTypename().toString().equals(click_bean.getAccount().toString())){
                        if (click_bean.getKind() == 0){
                            bean.setMoney(bean.getMoney()+click_bean.getMoney());
                        }else bean.setMoney(bean.getMoney()-click_bean.getMoney());
                        DBManager.alterItemFromAccmanagetbById(bean.getId(),bean);
                    }
                }
                DBManager.deleteItemFromAccounttbById(click_bean.getId());
                adapter.notifyDataSetChanged();
                MainActivity.setTopTvShow();

                cancel();
                break;
            case R.id.dialog_choice_context:
                intent = new Intent(getContext(), AccountContextActivity.class);
                intent.putExtra("click_bean", click_bean);
                getContext().startActivity(intent);
                cancel();
                break;
            case R.id.dialog_choice_alter:
                intent = new Intent(getContext(),RecordActivity.class);
                intent.putExtra("type",1);
                intent.putExtra("id",click_bean.getId());
                intent.putExtra("money",click_bean.getMoney());
                intent.putExtra("click_bean",click_bean);
                getContext().startActivity(intent);
                cancel();
                break;
            case R.id.dialog_choice_changed:
                cancel();
                break;
        }
    }

}
